import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.text.Element;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


//class InterWindow
final class InterWindow extends JPanel implements Observer{
  private final JEditorPane textArea;
  private UndoableEdit edit;
  private final Doer frame;
  private final JLabel line;
  private boolean saved=true;
  private char c='\0';
  private final JPopupMenu popMenu=new JPopupMenu();
  private final DocumentListener doc=new Doc();

  private String direction;

  InterWindow(Doer frame) {

    super(new BorderLayout());

    this.frame=frame;
    direction=frame.getDirection();

    textArea = new JEditorPane();
   
	try{
	  textArea.setFont(frame.getFont());
      RobocodeEditorKit editorKit = new RobocodeEditorKit();
      textArea.setEditorKitForContentType("text/java", editorKit);
      textArea.setContentType("text/java");
    }
    catch(Throwable e){
      e.printStackTrace();
    }
    textArea.getDocument().addDocumentListener(doc);

    JScrollPane textScroller = new JScrollPane(textArea,
	                                       JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
	                                       JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					       

    line=new JLabel();
    line.setFont(frame.getFont());
    line.setVerticalAlignment(JLabel.TOP);
    line.setHorizontalAlignment(JLabel.RIGHT);
    line.setText("1");

    add(textScroller,BorderLayout.CENTER);
    add(line,BorderLayout.WEST);
    
    popMenu.add(frame.getCopyItem());
    popMenu.add(frame.getCutItem());
    popMenu.add(frame.getPasteItem());

    textArea.addMouseListener(new MouseAdapter(){
                       public void mouseReleased(MouseEvent e){
                         if(e.isPopupTrigger()){
			   popMenu.show(e.getComponent(),e.getX(),e.getY());
			 }
		       }
		       public void mousePressed(MouseEvent e){
                         if(e.isPopupTrigger()){
			   popMenu.show(e.getComponent(),e.getX(),e.getY());
			 }
		       }
                     });

   final JLabel label=frame.getStatusBar().getLabel();
   textArea.addCaretListener(new CaretListener(){
                               public void caretUpdate(CaretEvent e) {
	   	                 try{
						   Element root = textArea.getDocument().getDefaultRootElement();
						   int li=root.getElementIndex(textArea.getCaretPosition());
						   int end =root.getElementIndex(root.getEndOffset());
	   	                   label.setText("L:"+(li+1)+" / "+(end+1));
	                         }
	                         catch(Exception eB){
				   System.out.println("Io Wrong");
	                         }
	   	               }
	                     });

    textArea.getDocument().addUndoableEditListener(new UndoableEditListener(){
       public void undoableEditHappened(UndoableEditEvent ev){
    	edit = ev.getEdit();
       }
    });
  }

  public void update(Observable o,Object font){
    textArea.setFont((Font)font);
    line.setFont((Font)font);
  }

  public JTextArea getTextArea(){
    return new JTextArea(textArea.getDocument());
  }

  public String getLine(){
    return line.getText();
  }
  public void setLine(String text){
    line.setText(text);
  }
  public String getDirection(){
    return direction;
  }
  public void setDirection(String s){
    direction=s;
  }
  public boolean isSaved(){
    return saved;
  }
  public void setSaved(boolean flag){
    saved=flag;
  }
  public UndoableEdit getDo(){
    return edit;
  }

  public void saveTest(){
    if(frame.getSaveItem().isEnabled()==false){
      frame.getSaveItem().setEnabled(true);
      saved=false;
      frame.setTitle(frame.getTitle()+'*');
    }
  }

  //class DocumentListener
  class Doc implements DocumentListener{
    public void changedUpdate(DocumentEvent e){}
    public void insertUpdate(DocumentEvent e){
      saveTest();
    }
    public void removeUpdate(DocumentEvent e){
      saveTest();
    }
  }
}
